package com.syqc.monitor.ui.alarm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.AlarmCount
import com.syqc.net.ServiceHelper

/**
 *@author joker
 *@time   2023/12/28 16:32
 *@desc
 */
class AlarmTypeVM : AutoDisposeViewModel() {
    val allAlarm = ArrayList<AlarmCount>()
    private val alarmList = ArrayList<AlarmCount>()

    private val _alarmsLive = MutableLiveData<ArrayList<AlarmCount>>()
    val alarmsLive: LiveData<ArrayList<AlarmCount>> = _alarmsLive

    fun getAlarmData() {
        ServiceHelper.getRepository().getAlarmCount().autoDispose(this)
            .subscribe({
                alarmList.clear()
                allAlarm.clear()
                allAlarm.addAll(it)
                val list = it.filter { item -> item.alarmNum > 0 }
                alarmList.addAll(list)
                alarmList.sort()
                search(upKey)
            }, error)
    }

    private var upKey: String = ""
    fun search(key: String) {
        upKey = key.uppercase()
        if (key.isEmpty()) {
            _alarmsLive.postValue(alarmList)
        } else {
            _alarmsLive.postValue(alarmList.filter {
                it.alarmName.uppercase().contains(upKey)
            } as ArrayList<AlarmCount>?)
        }
    }
}